     private Node prev,head;
    //中序遍历后结点的值是有序，因此利用中序遍历修改结点的left和right指向
    public void inOrderTraversal(Node root){
        if(root == null) return ;
        inOrderTraversal(root.left);
        root.left = prev;
        if(prev != null){
            prev.right = root;
        }else{
            head = root;
        }
        prev = root;
        inOrderTraversal(root.right);
    }

    public Node treeToDoublyList(Node root) {
        if(root == null) return null;
        inOrderTraversal(root); //转成排序的双向链表
        head.left = prev; //头结点前驱指向尾结点
        prev.right = head; //尾结点后继指向头结点
        return head; //返回排序后链表的头结点
    }
